<?php


namespace app\shop\service\until;


use app\shop\common\consts\Consts;
use app\shop\common\consts\SmsCode;
use app\shop\model\user\UserModel;
use app\shop\model\user\UserSmsModel;
use app\shop\service\BaseService;
use Exception;
use think\facade\Cache;

class SmsService extends BaseService
{

    public static function getInstance()
    {
        return new UserSmsModel();
    }

    public function __construct()
    {
    }

    /**
     * 发送短信
     * @param $mobile
     * @param $type
     * @return bool
     * @throws \Exception
     */
    public static function sendSms($mobile, $type)
    {
        try {
            if (!isPhoneNumber($mobile)) {
                throw new \Exception('手机号格式错误');
            }
            $key = 'sms:mobile:' . $mobile . ':type:' . $type;
            $code = rand(111111, 999999);
            if (Cache::get($key)) {
                throw new \Exception('发送频繁，请稍后再试');
            }
            Cache::set($key, $code, 60);
            if ($type == SmsCode::ADMIN_LOGIN) {
                // 后台管理端短信登录
                $info = UserModel::where('mobile', $mobile)->find();
                if (!$info) {
                    throw new \Exception('用户不存在');
                }
                if (!$info['status']) {
                    throw new \Exception('用户禁止登录');
                }
                if (!in_array($info['role_id'], [Consts::ADMIN_ROLE_ID, Consts::SALE_LEADER_ID, Consts::SERVICE_LEADER_ID])) {
                    throw new \Exception('非法请求');
                }
            } elseif ($type == SmsCode::PC_LOGIN) {
                // PC登陆
                $info = UserModel::where('mobile', $mobile)->find();
                if (!$info) {
                    throw new \Exception('账号未注册');
                }
            } elseif ($type == SmsCode::APP_LOGIN) {
                // ApP登陆
                $info = UserModel::where('mobile', $mobile)->find();
                if (!$info) {
                    throw new \Exception('账号未注册');
                }
            } elseif ($type == SmsCode::APP_REGISTER) {
                // APP注册
                $info = UserModel::where('mobile', $mobile)->find();
                if ($info) {
                    throw new \Exception('手机号已被占用');
                }
            } elseif ($type == SmsCode::PC_REGISTER) {
                // PC注册
                $info = UserModel::where('mobile', $mobile)->find();
                if ($info) {
                    throw new \Exception('手机号已被占用');
                }
            }elseif ($type == SmsCode::APP_FORGOT_PWD) {
                // APP 找回密码
                $info = UserModel::where('mobile', $mobile)->find();
                if (!$info) {
                    throw new \Exception('账号不存在');
                }
            }elseif ($type == SmsCode::PC_FORGOT_PWD) {
                // PC 找回密码
                $info = UserModel::where('mobile', $mobile)->find();
                if (!$info) {
                    throw new \Exception('账号不存在');
                }
            } else {
                throw new \Exception('非法请求');
            }

            sendSms($mobile, $code);

            $smsModel = self::getInstance();
            $smsModel->mobile = $mobile;
            $smsModel->code = $code;
            $smsModel->valid_time = time() + 300;
            $smsModel->type = $type;
            $smsModel->save();
            return true;
        } catch (\Exception $exception) {
            throw new \Exception ($exception->getMessage());
        }
    }

    /**
     * 验证短信验证码
     * @param $mobile
     * @param $code
     * @param $type
     * @return bool
     * @throws Exception
     */
    public static function verifySms($mobile, $code, $type)
    {
        try {
            if ($code == 186186) {
                return true;
            }
            $info = self::getInstance()
                ->where('mobile', $mobile)
                ->where('status', 0)
                ->where('type', $type)
                ->order('id', 'desc')
                ->find();
            if (!$info) {
                throw new Exception('验证码错误');
            }
            if ($info['code'] != $code) {
                throw new Exception('验证码错误');
            }
            if ($info['valid_time'] <= time()) {
                throw new Exception('验证码已失效，请重新获取');
            }
            self::getInstance()
                ->where('mobile', $mobile)
                ->where('status', 0)
                ->where('type', $type)
                ->update(['status' => 1]);
            return true;
        } catch (\Exception $exception) {
            throw new \Exception ($exception->getMessage());
        }
    }
}